﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Main : MonoBehaviour {

    public List<Ingredient> Glasses;
    public List<Ingredient> Liquids;
    public List<Ingredient> Extras;

    public List<Recipe> Recipes;

    public List<Ingredient> Workspace = new List<Ingredient>();
	public List<TextMesh> workspaceLocation = new List<TextMesh> ();
       
	public DoneButton doneBtn;

    public List<RemoveButtonScript> WorkspaceBttns;
    public List<ButtonA> IngredientBttns;

	//dealing with the random drink
	public int ranNum;
	public TextMesh drinkNameText, Message;
	public int score;
	public float percentage;
	public float totalPercentage;
	public int orderWrong;

	public timer timer;

	public List<ButtonA> aniList;
	public int playAniTempNo;

    List<Ingredient> CurIngs;

	public AudioClip[] sounds = new AudioClip[2];

    void setIngs(List<Ingredient> ins) {
        CurIngs = ins;
        for(int i = 0;i < IngredientBttns.Count;i++) {
            var b =  IngredientBttns[i];
        }
        
    }

     void Awake() {
	
        int i = 0;
        foreach(RemoveButtonScript ba in WorkspaceBttns) {
            ba.I = i++;
			ba.CallBack = removeItem;
        }
        i = 0;
        foreach(ButtonA ba in IngredientBttns) { 
            ba.I = i++;

			ba.nameCallBack = assignitem;
        }

        
    }

	void Start () {
        
		nextDrink ();
	}


	public void nextDrink()
	{
		clear();
		ranNum = Random.Range(0,Recipes.Count);
		drinkNameText.text = Recipes[ranNum].Name;
		timer.resetTimer ();
	}
	
	public void compare()
	{
		timer.pause = true;
		Debug.Log ("compare");
		percentage = 0;
		orderWrong = 0;
		score = 0;
        aniList.Clear();
        playAniTempNo = 0;
		foreach(var curIng in Recipes[ranNum].Ings)
		{
        
			//Debug.Log(curIng.Name);
			foreach (var curWrk in Workspace) {
				//Debug.Log (curWrk.Name);
				if (curIng.Name == curWrk.Name) {
					score++;
					break;
				}
			}
		}

        foreach (var curWrk in Workspace) {
            foreach (var buttn in IngredientBttns) {
				if (buttn.name == curWrk.Name) {
					aniList.Add( buttn);
					break;
				}
			}
        }
		//insertAnimations();
		runAnimations();
		//Debug.Log (score);
		//Debug.Log (Recipes [ranNum].Ings.Count);
		percentage = (score * 100)/ Recipes[ranNum].Ings.Count;

		if (percentage == 100) {
			audio.clip = sounds[0];
			audio.Play();
				}
		else if (percentage == 0) {
			audio.clip = sounds[1];
			audio.Play();
				}

        Message.text = percentage +"% correct  ";


	}

	public void insertAnimations ()
	{
		int i = 0;
		foreach (var curWrk in Workspace) {
			foreach (var buttn in IngredientBttns) {
				if (buttn.name == curWrk.Name) {
					aniList [i] = buttn;
					i++;
				}
				if (curWrk.Name == null) {
					return;
				}
			}
		}
	}

	public void runAnimations()
	{
        Debug.Log( " run anim "+playAniTempNo +"   "+ aniList.Count);
        if(playAniTempNo >= aniList.Count) {
            foreach( ButtonA ba in aniList ) {
                ba.GetComponent<Animator>().SetTrigger("reset");

            }
			nextDrink();
            return;
        }

        var a = aniList [playAniTempNo];
        a.GetComponent<Animator>().SetTrigger("runAnimation");
        Debug.Log( "run anim " + aniList [playAniTempNo].name );
		//aniList [playAniTempNo].GetComponent<Animation> ().Play ();

		//aniList [playAniTempNo].animation.AddClip (aniList[playAniTempNo].move, "move");
		//aniList [playAniTempNo].animation.Play ("move");
		
			playAniTempNo++;
				


	}

    public void clear() {
		Debug.Log ("clear");
        for (int i = 0; i < Workspace.Count; i++) {
			removeItem(i);
				}
		foreach (var buttn in IngredientBttns) {
			buttn.GetComponent<Animator> ().SetTrigger ("reset");									
		}
		//playAniTempNo = 0;
    }

	void removeItem (int index)
	{
		Workspace [index].Name= "";
		workspaceLocation [index].text = "";
	}

	bool findBlank(Ingredient ingre)
	{
		if (ingre.Name.CompareTo("")==0) {
			return true;
		}
		return false;
	}
	void assignitem (string name)
	{
		int index = Workspace.FindIndex(findBlank);
		if (index>=0)
		{
			Workspace[index].Name=name;
			workspaceLocation[index].text=Workspace[index].Name;
		}
	}

    void addIng( int i ) {
    }

    void clearIng( int i ) {
		Debug.Log( "clear  "+i );
        if( i >= Workspace.Count ) return;
		if( i == 0 ) { clear();return;}

        for(int j = i+1;j < Workspace.Count;i = j++) {
            Workspace[i] = Workspace[j];
        }
        Workspace.RemoveAt(i);
    }

    
}

